package org.jeecg.modules.wlhy.customer.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerAddressReceive;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerAddressSend;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerAddressReceiveService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.wlhy.customer.service.IHyCustomerAddressSendService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hyproject.service.IHyProjectService;
import org.jeecg.modules.wlhy.order.entity.HyOrder;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: hy_customer_address_receive
 * @Author: jeecg-boot
 * @Date:   2020-12-02
 * @Version: V1.0
 */
@Api(tags="hy_customer_address_receive")
@RestController
@RequestMapping("/customer/hyCustomerAddressReceive")
@Slf4j
public class HyCustomerAddressReceiveController extends JeecgController<HyCustomerAddressReceive, IHyCustomerAddressReceiveService> {
	@Autowired
	private IHyCustomerAddressReceiveService hyCustomerAddressReceiveService;

	@Autowired
	 private IHyCustomerAddressSendService hyCustomerAddressSendService;

	 @Autowired
	 private UserUtil userUtil;

	 @Autowired
	 private IHyProjectService hyProjectService;

	/**
	 * 分页列表查询
	 *
	 * @param hyCustomerAddressReceive
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */

	@ApiOperation(value="hy_customer_address_receive-分页列表查询", notes="hy_customer_address_receive-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyCustomerAddressReceive hyCustomerAddressReceive,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {

		QueryWrapper<HyCustomerAddressReceive> queryWrapper = new QueryWrapper<>();
		if(StringUtils.isNotBlank(hyCustomerAddressReceive.getAddressName())){
			queryWrapper.like("address_name",hyCustomerAddressReceive.getAddressName().trim());
			hyCustomerAddressReceive.setAddressName("");
		}
		HyCustomer customer = userUtil.getCustomer();
		if(customer!=null){
			queryWrapper.eq("customer_id",customer.getId());
		}




		QueryGenerator.initQueryWrapper(queryWrapper , hyCustomerAddressReceive, req.getParameterMap());
		Page<HyCustomerAddressReceive> page = new Page<HyCustomerAddressReceive>(pageNo, pageSize);
		IPage<HyCustomerAddressReceive> pageList = hyCustomerAddressReceiveService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param hyCustomerAddressReceive
	 * @return
	 */
	@AutoLog(value = "hy_customer_address_receive-添加")
	@ApiOperation(value="hy_customer_address_receive-添加", notes="hy_customer_address_receive-添加")
	@PostMapping(value = "/add")
	@RequiresPermissions("customer:receiveAddr:add")
	public Result<?> add(@RequestBody HyCustomerAddressReceive hyCustomerAddressReceive) {
		if("1".equals(hyCustomerAddressReceive.getIzToCommon())){
			if(StringUtils.isEmpty(hyCustomerAddressReceive.getCustomerId())){
				return Result.error("customerId不能为空");
			}
		}else {
			if (userUtil.isSys()) {
				if (StringUtils.isEmpty(hyCustomerAddressReceive.getCustomerId())) {
					return Result.error("customerId不能为空");
				}
			}else {
				HyCustomer customer = userUtil.getCustomer();
				if (customer == null) {
					return Result.error("货主信息不存在");
				}
				hyCustomerAddressReceive.setCustomerId(customer.getId());
			}
			if ("1".equals(hyCustomerAddressReceive.getIstb())) {
				HyCustomerAddressSend hyCustomerAddressSend = new HyCustomerAddressSend();
				BeanUtils.copyProperties(hyCustomerAddressReceive, hyCustomerAddressSend);
				hyCustomerAddressReceive.setAreaCode(hyCustomerAddressReceive.getAreaCode().substring(0,6));
				hyCustomerAddressSendService.save(hyCustomerAddressSend);
			}
		}
		hyCustomerAddressReceive.setAreaCode(hyCustomerAddressReceive.getAreaCode().substring(0,6));
		hyCustomerAddressReceiveService.save(hyCustomerAddressReceive);
		return Result.OK("添加成功！" ,hyCustomerAddressReceive );
	}

	/**
	 *  编辑
	 *
	 * @param hyCustomerAddressReceive
	 * @return
	 */
	@AutoLog(value = "hy_customer_address_receive-编辑")
	@ApiOperation(value="hy_customer_address_receive-编辑", notes="hy_customer_address_receive-编辑")
	@PutMapping(value = "/edit")
	@RequiresPermissions("customer:receiveAddr:edit")
	@CacheEvict(value = CacheConstant.SYS_DICT_TABLE_CACHE ,key = "'SimpleKey [hy_customer_address_receive,address_name,id,' + #hyCustomerAddressReceive.id + ']'")
	public Result<?> edit(@RequestBody HyCustomerAddressReceive hyCustomerAddressReceive) {
		HyCustomer customer = userUtil.getCustomer();
		if(customer!=null){
			if(!StringUtils.equals(hyCustomerAddressReceive.getCustomerId(),customer.getId())){
				return Result.error("数据不存在!");
			}
		}
		hyCustomerAddressReceive.setAreaCode(hyCustomerAddressReceive.getAreaCode().substring(0,6));
		hyCustomerAddressReceiveService.updateById(hyCustomerAddressReceive);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "hy_customer_address_receive-通过id删除")
	@ApiOperation(value="hy_customer_address_receive-通过id删除", notes="hy_customer_address_receive-通过id删除")
	@DeleteMapping(value = "/delete")
	@RequiresPermissions("customer:receiveAddr:delete")
	@CacheEvict(value = CacheConstant.SYS_DICT_TABLE_CACHE ,key = "'SimpleKey [hy_customer_address_receive,address_name,id,' + #id + ']'")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {

		HyCustomer customer = userUtil.getCustomer();
		HyCustomerAddressReceive addressReceive = hyCustomerAddressReceiveService.getById(id);
		if(addressReceive == null){
			return Result.error("数据不存在!");
		}

		if(customer!=null){
			if(!StringUtils.equals(addressReceive.getCustomerId(),customer.getId())){
				return Result.error("数据不存在!");
			}
		}

		hyCustomerAddressReceiveService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "hy_customer_address_receive-批量删除")
	@ApiOperation(value="hy_customer_address_receive-批量删除", notes="hy_customer_address_receive-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	@RequiresPermissions("customer:receiveAddr:deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hyCustomerAddressReceiveService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="hy_customer_address_receive-通过id查询", notes="hy_customer_address_receive-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		HyCustomerAddressReceive hyCustomerAddressReceive = hyCustomerAddressReceiveService.getById(id);
		if(hyCustomerAddressReceive==null) {
			return Result.error("未找到对应数据");
		}
//		HyCustomer customer = userUtil.getCustomer();
//		if(customer!=null){
//			if(!StringUtils.equals(hyCustomerAddressReceive.getCustomerId(),customer.getId())){
//				return Result.error("数据不存在!");
//			}
//		}
		return Result.OK(hyCustomerAddressReceive);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hyCustomerAddressReceive
    */
    @RequestMapping(value = "/exportXls")
	@RequiresPermissions("customer:receiveAddr:exportXls")
	public ModelAndView exportXls(HttpServletRequest request, HyCustomerAddressReceive hyCustomerAddressReceive) {
        return super.exportXls(request, hyCustomerAddressReceive, HyCustomerAddressReceive.class, "hy_customer_address_receive");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	@RequiresPermissions("customer:receiveAddr:importExcel")
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HyCustomerAddressReceive.class);
    }

}
